//
//  ChatCell.m
//  Topic1
//
//  Created by 孙添凤 on 16/12/7.
//  Copyright © 2016年 孙添凤. All rights reserved.
//

#import "ChatCell.h"
#import "Masonry.h"
#import "UIColor+CL.h"
#define KWIDTH ([UIScreen mainScreen].bounds.size.width)
@implementation ChatCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


-(instancetype)initIdentifier:(NSString *)identifer{
    self=  [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    if(self){
        UIView *superView = [self createUI];
        [self.contentView addSubview:superView];
        UIView *superview = self.contentView;
        UIEdgeInsets edge = UIEdgeInsetsMake(0, 0, 0, 0);
        [superView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(superview).insets(edge);
        }];
        
    }
    return self;
}

-(UIView *)createUI{
    UIView *baseView = [[UIView alloc]init];
    
    
    _img =[[UIImageView alloc]init];
    [baseView addSubview:_img];
    [_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(baseView.mas_left).offset(20);
        make.top.equalTo(baseView.mas_top).offset(5);
        make.width.equalTo(@(30));
        make.height.equalTo(@(30));
    }];
    _img.image =[UIImage imageNamed:@"my_default_portrait_b"];
    
    
    _leftName =[[UILabel alloc]init];
    [baseView addSubview:_leftName];
    _leftName.text = @"gogog";
    [_leftName sizeToFit];
    [_leftName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_img.mas_right).offset(10);
        make.top.equalTo(_img.mas_top);
        make.right.equalTo(baseView.mas_right).offset(-20);
    }];
    _leftName.font =[UIFont systemFontOfSize:10];
    
    
    _image =[[UIImageView alloc]init];
    [baseView addSubview:_image];
    _image.backgroundColor =[UIColor colorWithHex:0xededed];
    _image.layer.cornerRadius = 10;
    [_image sizeToFit];
    [_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_img.mas_right).offset(10);
        make.top.equalTo(_leftName.mas_bottom).offset(5);
//        make.right.equalTo(baseView.mas_right).offset(-20);
        make.width.equalTo(@(KWIDTH-(74+34)));
//        make.right.equalTo(baseView.mas_right).offset(-34);
        make.height.equalTo(@(30));
    }];
    
    _time =[[UILabel alloc]init];
    [baseView addSubview:_time];
    _time.text = @"gogog";
    [_time sizeToFit];
    [_time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_image.mas_left);
        make.top.equalTo(_image.mas_bottom).offset(5);
        make.width.equalTo(@(200));
        make.height.equalTo(@(13));
    }];
    _time.font =[UIFont systemFontOfSize:13];
    _time.textAlignment = NSTextAlignmentLeft;
    
    _leftArrow = [[UIImageView alloc]init];
    [baseView addSubview:_leftArrow];
    [_leftArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_image.mas_top).offset(13);
        make.right.equalTo(_image.mas_left);
        make.width.equalTo(@(5));
        make.height.equalTo(@(6));
    }];
    _leftArrow.image =[UIImage imageNamed:@"left"];
    
    _leftLa =[[UILabel alloc]init];
    [baseView addSubview:_leftLa];
    _leftLa.text = @"gogog";
    _leftLa.backgroundColor =[UIColor clearColor];
    [_leftLa sizeToFit];
    [_leftLa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_image.mas_left).offset(13);
        make.top.equalTo(_image.mas_top).offset(13);
        make.right.equalTo(_image.mas_right).offset(-13);
    }];
    _leftLa.numberOfLines = 0;
    _leftLa.font =[UIFont systemFontOfSize:16];
    _leftLa.textColor =[UIColor colorWithHex:0x333333];
    
    
    _img2 =[[UIImageView alloc]init];
    [baseView addSubview:_img2];
    [_img2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(baseView.mas_right).offset(-20);
        make.top.equalTo(baseView.mas_top).offset(5);
        make.width.equalTo(@(30));
        make.height.equalTo(@(30));
    }];
    _img2.image =[UIImage imageNamed:@"my_default_portrait_b"];

    
    _rightName =[[UILabel alloc]init];
    [baseView addSubview:_rightName];
    _rightName.text = @"gogog";
    [_rightName sizeToFit];
    [_rightName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_img2.mas_left).offset(-10);
        make.top.equalTo(_img2.mas_top);
        make.width.equalTo(@(200));
    }];
    _rightName.font =[UIFont systemFontOfSize:10];
    _rightName.textAlignment = NSTextAlignmentRight;
    
    
    _image2 =[[UIImageView alloc]init];
    [baseView addSubview:_image2];
    _image2.backgroundColor =[UIColor colorWithHex:0x48e49d];
    _image2.layer.cornerRadius = 10;
    [_image2 sizeToFit];
    [_image2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_img2.mas_left).offset(-10);
        make.top.equalTo(_rightName.mas_bottom).offset(5);
        //        make.right.equalTo(baseView.mas_right).offset(-20);
//        make.left.equalTo(baseView.mas_left).offset(34);
        make.width.equalTo(@(KWIDTH-(74+34)));
        make.height.equalTo(@(30));
    }];

    _time2 =[[UILabel alloc]init];
    [baseView addSubview:_time2];
    _time2.text = @"gogog";
    [_time2 sizeToFit];
    [_time2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_image2.mas_right);
        make.top.equalTo(_image2.mas_bottom).offset(5);
                make.width.equalTo(@(200));
        make.height.equalTo(@(13));
    }];
    _time2.font =[UIFont systemFontOfSize:13];
    _time2.textAlignment = NSTextAlignmentRight;
    _time.textColor = [UIColor colorWithHex:0xb7b7b7];
    _time2.textColor = [UIColor colorWithHex:0xb7b7b7];
    
    _rightArrow = [[UIImageView alloc]init];
    [baseView addSubview:_rightArrow];
    [_rightArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_image2.mas_top).offset(13);
        make.left.equalTo(_image2.mas_right);
        make.width.equalTo(@(5));
        make.height.equalTo(@(6));
    }];
    _rightArrow.image =[UIImage imageNamed:@"right"];
    
    _rightLa =[[UILabel alloc]init];
    [baseView addSubview:_rightLa];
    _rightLa.text = @"gogog";
    _rightLa.textAlignment = NSTextAlignmentRight;
    [_rightLa sizeToFit];
    [_rightLa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_image2.mas_right).offset(-13);
        make.top.equalTo(_image2.mas_top).offset(13);
        make.left.equalTo(_image2.mas_left).offset(13);
    }];
    _rightLa.numberOfLines = 0;
    _rightLa.backgroundColor  = [UIColor clearColor];
    _rightLa.textColor =[UIColor colorWithHex:0x333333];
    _rightLa.font = [UIFont systemFontOfSize:16];
    //    [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    _rightLa.hidden = YES;
//    _rightName.hidden = YES;
//    _leftLa.hidden = YES;
//    _leftName.hidden = YES;
    
    _leftVoice = [[UIImageView alloc]init];
    [_image addSubview:_leftVoice];
    [_leftVoice sizeToFit];
    [_leftVoice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_image.mas_left).offset(12);
        make.centerY.equalTo(_image.mas_centerY);
    }];
    _leftVoice.image =[UIImage imageNamed:@"play_voice_l"];
    
    
    _rightVoice = [[UIImageView alloc]init];
    [_image2 addSubview:_rightVoice];
    [_rightVoice sizeToFit];
    [_rightVoice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_image2.mas_right).offset(-12);
        make.centerY.equalTo(_image2.mas_centerY);
    }];
    _rightVoice.image =[UIImage imageNamed:@"play_voice_r"];
    
    _leftTime = [[UILabel alloc]init];
    [_image addSubview:_leftTime];
    [_leftTime sizeToFit];
    [_leftTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_image.mas_right).offset(-12);
        make.centerY.equalTo(_image.mas_centerY);
        make.width.equalTo(@(20));
        make.height.equalTo(@(20));
    }];
    _leftTime.font =[UIFont systemFontOfSize:13];
    _leftTime.textColor =[UIColor colorWithHex:0x208959];
    
    
    _rightTime = [[UILabel alloc]init];
    [_image2 addSubview:_rightTime];
    [_rightTime sizeToFit];
    [_rightTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_image2.mas_left).offset(12);
        make.centerY.equalTo(_image2.mas_centerY);
        make.width.equalTo(@(40));
        make.height.equalTo(@(20));
    }];
    _rightTime.font =[UIFont systemFontOfSize:13];
    _rightTime.textColor =[UIColor colorWithHex:0x208959];
    
    _rightPhoto = [[UIImageView alloc]init];
    [_image2 addSubview:_rightPhoto];
    [_rightPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_image2.mas_left).offset(3);
        make.right.equalTo(_image2.mas_right).offset(-3);
        make.top.equalTo(_image2.mas_top).offset(3);
        make.bottom.equalTo(_image2.mas_bottom).offset(-3);
    }];
    _rightPhoto.image =[UIImage imageNamed:@"990"];
    _rightPhoto.layer.cornerRadius = 10;
    _rightPhoto.layer.masksToBounds = YES;
    
    return baseView;
}

//- (void)layoutSubviews{
//    NSLog(@"cdddd");
//    NSString *ss =[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"];
//    if(![[NSString stringWithFormat:@"%@",_model.AFM_12]isEqualToString:ss]){
//        _rightName.hidden = YES;
//        _rightLa.hidden = YES;
//        _image2.hidden = YES;
//        //        cell.img2.hidden = YES;
//    }else{
//        [self layoutSubviews];
//        _leftLa.hidden = YES;
//        _leftName.hidden = YES;
//        _image.hidden = YES;
//        NSLog(@"__________**********)_________");
//        //        cell.img.hidden = YES;
//    }
//
//}

-(void)setModel:(TopicObjModel *)model{
    _model = model;
    
    
        if(!model.ismySelf){
            self.leftLa.text =  model.MESSCONTENT;
            self.leftName.text = model.SEND_NAME;
            self.time.text = model.AFM_12;
            self.leftTime.text = [NSString stringWithFormat:@"%ld\"",model.voiceTime];
            
            _time2.hidden = YES;
            _time.hidden = NO;
            _rightName.hidden = YES;
            _rightLa.hidden = YES;
            _image2.hidden = YES;
            _img2.hidden = YES;
            _rightArrow.hidden = YES;
    
    
            _leftLa.hidden = NO;
            _leftName.hidden = NO;
            _image.hidden = NO;
            _leftArrow.hidden = NO;
            _img.hidden = NO;
            if(_model.isVoice){
              _leftLa.hidden = YES;
              _leftVoice.hidden = NO;
                _leftTime.hidden = NO;
            }else{
                _leftLa.hidden = NO;
                _leftVoice.hidden = YES;
                _leftTime.hidden = YES;
            }
            
        }else{
            
            self.rightLa.text =  model.MESSCONTENT;
            self.rightName.text = model.SEND_NAME;
            self.time2.text = model.AFM_12;
            self.rightTime.text = [NSString stringWithFormat:@"%ld\"",model.voiceTime];
            
            _time2.hidden = NO;
            _time.hidden = YES;
            _leftLa.hidden = YES;
            _leftName.hidden = YES;
            _leftArrow.hidden = YES;
            _image.hidden = YES;
            _rightName.hidden = NO;
            _rightLa.hidden = NO;
            _image2.hidden = NO;
            _img.hidden = YES;
            _img2.hidden = NO;
            _rightArrow.hidden = NO;
            if(_model.isVoice){
                _rightLa.hidden = YES;
                _rightVoice.hidden = NO;
                _rightTime.hidden = NO;
            }else{
                _rightLa.hidden = NO;
                _rightVoice.hidden = YES;
                _rightTime.hidden = YES;
            }
            
            if(_model.isphoto){
                _rightLa.hidden = YES;
                _rightVoice.hidden = YES;
                _rightTime.hidden = YES;
                _rightPhoto.hidden = NO;
            }else{
                if(_model.isVoice){
                    _rightLa.hidden = YES;
                    _rightVoice.hidden = NO;
                    _rightTime.hidden = NO;
                    _rightPhoto.hidden = YES;
                }else{
                    _rightLa.hidden = NO;
                    _rightVoice.hidden = YES;
                    _rightTime.hidden = YES;
                    _rightPhoto.hidden = YES;
                }
                
            }
            
        }

    
    [self setNnnnnn:model.MESSCONTENT];
//    [self setUserID:[NSString stringWithFormat:@"%@",model.AFM_8]];
//    NSString *ss =[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"];
//    NSString *str = [NSString stringWithFormat:@"%@",model.AFM_8];
    
    
}

-(void)setUserID:(NSString *)userID{


}


-(void)setNnnnnn:(NSString *)nnnnnn{
    NSLog(@"%f",KWIDTH);
    UILabel *la =[[UILabel alloc]init];
    la.text = nnnnnn;
    la.numberOfLines = 0;
    
    int a=0;
    if([nnnnnn containsString:@"音频"]){
        NSArray *arr = [nnnnnn componentsSeparatedByString:@"."];
        a = [arr[0] intValue];
    }
    float ww = 30;
    float hh = [la sizeThatFits:CGSizeMake(KWIDTH-(74+26+34), CGFLOAT_MAX)].height;//动态计算高度
    
    if([nnnnnn containsString:@"图片"]){
        
        [_image mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(100));
            make.height.equalTo(@(150));
        }];
        
        [_image2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(100));
            make.height.equalTo(@(150));
        }];
        
    }else{
       
        if(hh<25){
            if(nnnnnn){
                // ww = [la sizeThatFits:CGSizeMake(CGFLOAT_MAX, 21)].width;//动态计算高度
                ww = [la sizeThatFits:CGSizeMake(CGFLOAT_MAX, 16)].width;//动态计算高度
            }else{
                
            }
            
            if(!a){
                [_image mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.equalTo(@(ww+25));
                    make.height.equalTo(@(hh+26));
                }];
                
                [_image2 mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.equalTo(@(ww+25));
                    make.height.equalTo(@(hh+26));
                }];
            }else{
                [_image mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.equalTo(@(25+(10*a)));
                    make.height.equalTo(@(hh+26));
                }];
                
                [_image2 mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.equalTo(@(25+(10*a)));
                    make.height.equalTo(@(hh+26));
                }];
                
            }
            
            //        [_time mas_updateConstraints:^(MASConstraintMaker *make) {
            //            make.right.equalTo(_image2.mas_right);
            //        }];
        }else{
            if(!a){
                [_image mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(hh+26));
                    make.width.equalTo(@(KWIDTH-(74+34)));
                }];
                [_image2 mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(hh+26));
                    make.width.equalTo(@(KWIDTH-(74+34)));
                }];
                
            }else{
                [_image mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(hh+26));
                    make.width.equalTo(@(25+(10*a)));
                }];
                [_image2 mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(hh+26));
                    make.width.equalTo(@(25+(10*a)));
                }];
            }
        }

        
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
